<?php
function ajoutComment($id_news, $pseudo, $message, $mail){
    global $bdd;

    if($_SESSION['login']){$ID_membre = $_SESSION['ID'];} else{ $ID_membre = null;}

    $req = $bdd->prepare("INSERT INTO news_comments(ID_news, pseudo, mail, message, ID_membre, ip, date) VALUES (:ID_news, :pseudo, :mail, :message, :ID_membre, :ip, NOW())");
    $req->execute(array("ID_news" => $id_news, "pseudo" => $pseudo, "mail" => $mail, "message" => $message,
        "ID_membre" => $ID_membre, "ip" => $_SERVER["REMOTE_ADDR"])) or die(print_r($req->errorInfo()));
    $req->closeCursor();
    return '';
}

function deleteComment($id_comment){
    global $bdd;

    $req= $bdd->prepare("DELETE FROM news_comments WHERE ID = ?");
    $req->execute(array($id_comment)) or die (print_r($req->errorInfo()));
    $req->closeCursor();
    return '';
}

function ajoutNews($titre, $auteur, $texte){
    global $bdd;

    $req = $bdd->prepare('INSERT INTO news (titre, auteur, date, texte) VALUES (:titre, :auteur, NOW(), :texte)');
    $req->execute(array('titre' => $titre, 'auteur' => $auteur, 'texte' => $texte)) or die(print_r($req->errorInfo()));
    $req->closeCursor();
    return '';
}

function updateNews($id, $titre, $auteur, $date, $texte){
    global $bdd;
    
    $req = $bdd->prepare('UPDATE news SET titre = :titre, auteur = :auteur, date = :date, texte = :texte WHERE ID = :id');
    $req->execute(array('titre' => $titre, 'auteur' => $auteur, 'date' => $date, 'texte' => $texte,
        'id' => $id)) or die(print_r($req->errorInfo()));
    $req->closeCursor();
    return '';
}

function deleteNews($id){
    global $bdd;

    $req = $bdd->prepare('DELETE FROM news WHERE ID=?');
    $req->execute(array($id)) or die(print_r($req->errorInfo()));
    $req->closeCursor();
}

function deleteCommentForNews($id_news){
    global $bdd;

    $req = $bdd->prepare("DELETE FROM news_comments WHERE ID_news = ?");
    $req->execute(array($id_news)) or die(print_r($req->errorInfo()));
    $req->closeCursor();
    return '';
}

function getComments($id_news, $debut, $nombre)
{
    global $bdd;
    $req = $bdd->prepare('SELECT *, DATE_FORMAT(date, \'%d/%m/%Y à %Hh%i\') AS dateFormated FROM news_comments WHERE ID_news = ? ORDER BY ID ASC LIMIT '.$debut.','.$nombre);
    $req->execute(array($id_news)) or die(print_r($req->errorInfo()));
    $comments = $req->fetchAll();
    $req->closeCursor();
    return $comments;
}
function getComment($id_comment)
{
    global $bdd;
    $req = $bdd->prepare('SELECT *, DATE_FORMAT(date, \'%d/%m/%Y à %Hh%i\') AS dateFormated FROM news_comments WHERE ID = ? LIMIT 0,1');
    $req->execute(array($id_comment)) or die(print_r($req->errorInfo()));
    $comment = $req->fetchAll();
    $req->closeCursor();
    return $comment;
}

function nbComments($id_news){
    global $bdd;
    $req = $bdd->prepare('SELECT COUNT(*) AS nbComments FROM `news_comments` WHERE ID_news = ?');
    $req->execute(array($id_news)) or die(print_r($req->errorInfo()));
    $result = $req->fetch();
    $nbComments = $result['nbComments'];
    $req->closeCursor();
    return $nbComments;
}

function getNews($debut, $limit, $tri)
{
    global $bdd;
    $req = $bdd->query('SELECT ID, titre, texte, DATE_FORMAT(date, \'%d/%m/%Y à %Hh%i\') AS date, date AS dateOrder,
        auteur FROM news ORDER BY '.$tri.' LIMIT '.($debut).','.$limit) or die(print_r($bdd->errorInfo()));
    $news = $req->fetchAll();
    $req->closeCursor();
    return $news;
}
function getNew($id)
{
    global $bdd;
    $req = $bdd->prepare('SELECT ID, titre, texte, DATE_FORMAT(date, \'%d/%m/%Y à %Hh%i\') AS date, date AS dateOrder,
        auteur FROM news WHERE ID = ?');
    $req->execute(array($id)) or die(print_r($req->errorInfo()));
    $news = $req->fetchAll();
    $req->closeCursor();
    return $news;
}

function getNombreNews(){
    global $bdd;
    $req = $bdd->query('SELECT COUNT(*) AS nbNews FROM `news`') or die(print_r($bdd->errorInfo()));
    $result = $req->fetch();
    $nbNews = $result['nbNews'];
    $req->closeCursor();
    return $nbNews;
}